const C = {};

// 获取鼠标在元素上的坐标
C.getOffset = (el) => {
  const mouse = { x: 0, y: 0 };

  el.addEventListener("mousemove", (ev) => {
    const { x, y } = C.eventWrapper(ev);
    mouse.x = x;
    mouse.y = y;
  });

  return mouse;
};

// 坐标系转换
C.eventWrapper = (ev) => {
  const { clientX, clientY, target } = ev;
  const { left, top } = target.getBoundingClientRect();
  return {
    x: clientX - left,
    y: clientY - top,
  };
};

// 角度转成弧度
C.toRad = (angle) => {
  return (angle * Math.PI) / 180;
};

// 弧度转成角度
C.toAngle = (rad) => {
  return (rad * 180) / Math.PI;
};

// 区间随机数字 arr =[10, 30], is_int = true
C.rp = (arr, is_int) => {
  const max = Math.max(...arr);
  const min = Math.min(...arr);
  const num = Math.random() * (max - min) + min;
  return is_int ? Math.round(num) : num;
};

// 随机颜色值
C.createColor = () => {
  return `rgb(${C.rp([0, 255], true)}, ${C.rp([0, 255], true)}, ${C.rp([0, 255], true)})`;
};

C.$ = (selector) => document.querySelector(selector);
